//
//  HNSH_SearchFriendsViewController.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/2/17.
//

#import "HNSH_SearchFriendsViewController.h"
#import "HNSH_SearchFriendsCell.h"
#import "HNSH_SearchFriendsNav.h"

#import "HNSH_ChatFriendDetailViewController.h"

@interface HNSH_SearchFriendsViewController ()
/** */
@property (nonatomic, strong)HNSH_SearchFriendsNav *searchNavView;
/** */
@property (nonatomic, copy)NSArray * dataArray;

@end

static NSString *HNSH_SearchFriendsCellID = @"HNSH_SearchFriendsCellID";

@implementation HNSH_SearchFriendsViewController

//MARK: Cycle Methods
- (void)viewDidLoad{
    [super viewDidLoad];
    
    [self showNavTitle:@"添加好友" showLeftItem:YES];
    
    [self addAllViews];
    
    [self configCollection];
}

//MARK: Myself Methods
- (void)addAllViews{
    [self.view addSubview:self.searchNavView];
}

- (void)configCollection {
    [self.collectionView registerClass:[HNSH_SearchFriendsCell class] forCellWithReuseIdentifier:HNSH_SearchFriendsCellID];
    [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(kNavBarAndStatusBarHeight+44, 0, 0, 0));
    }];
}

//MARK: 返回
- (void)back{
    [self.navigationController popViewControllerAnimated:YES];
}

//MARK: 搜索
- (void)searchWithKeyword:(NSString *)keyword{
    [HNSH_ChatNetServer requestRongCloudFriendshipSearchUserWithSearchKey:keyword ? : @"" success:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            NSMutableArray * arr = [NSMutableArray array];
            NSArray * list = responseObject.resultset;
            for (NSDictionary * lDic in list) {
                HNSH_SearchFriendsModel * model = [HNSH_SearchFriendsModel yy_modelWithDictionary:lDic];
                [arr addObject:model];
            }
            self.dataArray = arr.copy;
            [self.collectionView reloadData];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

//MARK: Getter Methods
- (HNSH_SearchFriendsNav *)searchNavView{
    if (!_searchNavView) {
        _searchNavView = [[HNSH_SearchFriendsNav alloc] initWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight, kScreenWidth, 44)];
        
        WeakSelf(weakSelf);
        _searchNavView.backBlock = ^{
            [weakSelf back];
        };
        _searchNavView.searchBlock = ^(NSString * _Nonnull keyword) {
            [weakSelf searchWithKeyword:keyword];
        };
    }
    return _searchNavView;
}


//MARK: UICollectionViewDelegate Methods
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HNSH_SearchFriendsCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:HNSH_SearchFriendsCellID forIndexPath:indexPath];
    HNSH_SearchFriendsModel * model = self.dataArray[indexPath.row];
    cell.model = model;
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(kScreenWidth, 54);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeZero;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    return CGSizeZero;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    HNSH_SearchFriendsModel * model = self.dataArray[indexPath.row];
    HNSH_ChatFriendDetailViewController * vc = [HNSH_ChatFriendDetailViewController new];
    vc.rcUserId = model.uid;
    [self.navigationController pushViewController:vc animated:YES];
}

@end
